using System;
using BitBenderGames;
using Gameplay.PVE;
using OWL.Rendering.HRP;

#if UNITY_EDITOR
using UnityEditor;
using Yoozoo.Core.Common;
#endif

using UnityEngine;
using UnityEngine.SceneManagement;
using Yoozoo.HRP.Runtime;
using Yoozoo.Gameplay.Liberty;
using ImageEffects;
using Yoozoo.Core.PathFinder;
using Yoozoo.Gta.Common;
using Yoozoo.Modules.InnerCity.TouchCamera;

namespace Yoozoo.Gameplay.City
{
    public class CityManager : MonoBehaviour
    {
        public static Action OnUpdateGangSetting;
        public static Action<int,string> OnUpdateTempGangSetting;
        public static Action OnUpdateWallStar;
        public static Action<int, int> OnUpdateWallStarSetting;
        
        public Transform cityRoot;
        public MobileTouchCamera mobileTouchCamera;
        public TouchCameraController touchCameraController;
        public InnerCityCameraComponent innerCityCameraComponent;

        public GgameGridGraph gridGraph;
        
        // 房间数据管理器
        public RoomDataManager RoomDataManager;
        // 触摸管理器
        public CityTouchManager CityTouchManager;
        // 事件管理器
        public CityEventManager CityEventManager;
        // LOD管理器
        public CityLODManager CityLODManager;
        // 渲染设置
        public CityRenderSettingMgr CityRenderSettingMgr;
        //氛围小人控制器
        public CityAIManager CityAIManager;
        //timeline控制器
        public TimelineManager TimelineManager => BlueprintUtils.TimelineManager;

        //灯光控制器
        public LightController LightController;
        //辅光控制
        public SetRimLightCameraDir setRimLightCamera;

        public static CityManager Instance { get; private set; }

        /// <summary>
        /// 场景物件动态加载控制器
        /// </summary>
        public SceneObjectLoadController SceneObjectLoadController;

        /// <summary>
        /// 场景探测
        /// </summary>
        public SceneDetectorBase SceneDetector;

        
        [SerializeField]
        private float limitZoom;
    
        [SerializeField]
        private float limitShadowZoom;
        
        private bool isLimit; // 是否达到限制的高度
        private bool isShadowLimit;//是否达到影子得限制高度
        private void Awake()
        {

#if UNITY_EDITOR
            Scene scene = SceneManager.GetActiveScene();
            if (scene.name == "RPG_CityScene")
            {
                CityGlobalValue.IS_RUNTIME_MODE = false;
                cityRoot = GameObject.Find("DataRoot").transform;
                mobileTouchCamera = FindObjectOfType<MobileTouchCamera>();
                //touchCameraController = FindObjectOfType<TouchCameraController>();
                //RoomDataManager = FindObjectOfType<RoomDataManager>();
                //CityTouchManager = FindObjectOfType<CityTouchManager>();
                //CityEventManager = FindObjectOfType<CityEventManager>();
                CityLODManager = FindObjectOfType<CityLODManager>();
                CityRenderSettingMgr = FindObjectOfType<CityRenderSettingMgr>();
                
                var renderSceneObj = GameObject.Instantiate( AssetDatabase.LoadAssetAtPath<GameObject>(
                    "Assets/ResourcesAssets/City/Preload/Prefabs/CityScene.prefab"));

                LightController = renderSceneObj.GetComponentInChildren<LightController>(true);
                var renderScene = renderSceneObj.GetComponent<HBaseRenderScene>();
                HRPHelper.EnableBaseScene(renderScene);

                // 手动启用渲染设置
                var renderListener = renderSceneObj.GetComponentInChildren<RenderComponentQualityListener>();
                var styleSetting = renderListener.styleSettings[renderListener.styleSettings.Count - 1];
                renderListener.ChangePostEffectStyle(styleSetting.style);
                
                var volumeListener = renderSceneObj.GetComponentInChildren<VolumeQualityListener>();
                var volumeSetting = volumeListener.styleSettings[volumeListener.styleSettings.Count - 1];
                volumeListener.ChangePostEffectStyle(volumeSetting.style);
                
                Init(null,null,null);
            }
#endif
            
            Instance = this;
        }
        
        public void Init(Transform dataRoot,Transform cameraRoot,Transform renderSceneObj)
        {
            if (dataRoot)
            {
                cityRoot = dataRoot;
            }

            if (cameraRoot)
            {
                mobileTouchCamera = cameraRoot.GetComponent<MobileTouchCamera>();
                mobileTouchCamera.OnLodChange += OnLodChange;
                mobileTouchCamera.OnZoomChange += OnZoomChange;
                innerCityCameraComponent = cameraRoot.GetComponent<InnerCityCameraComponent>();
            }
            
            // 初始化房间数据
            //var rooms= cityRoot.GetComponentsInChildren<RoomData>().ToList();
            //RoomDataManager.InitRooms(rooms);
            // 初始化触摸管理器
            //CityTouchManager.Init(mobileTouchCamera);
            // 初始化事件管理器
            //CityEventManager.Init(CityTouchManager,RoomDataManager);
            // 初始化LOD管理器
            //CityLODManager.Init(mobileTouchCamera,touchCameraController,CityEventManager);
            //初始化timeline管理器
            LightController = renderSceneObj.GetComponentInChildren<LightController>(true);
            setRimLightCamera = renderSceneObj.GetComponentInChildren<SetRimLightCameraDir>(true);
            Instance = this;
            CityAIUtils.CityManager = this;
        }
        

        private void Update()
        {
            if (Instance)
            {

                //CityLODManager.OnUpdate();

                if (SceneObjectLoadController != null && SceneDetector != null)
                {
                    SceneObjectLoadController.RefreshDetector(SceneDetector);
                }

                //CityLODManager.OnUpdate();

            }
        }

        public void UpdateWallStar()
        {
            OnUpdateWallStar?.Invoke();
        }
        
        public void UpdateWallStarSetting(int rootLevel , int starLevel)
        {
            OnUpdateWallStarSetting?.Invoke(rootLevel,starLevel);
        }
        
        public void UpdateGangSetting()
        {
            OnUpdateGangSetting?.Invoke();
        }

        public void UpdateTempGangSetting(int gangLogoIndex, string gangName)
        {
            OnUpdateTempGangSetting?.Invoke(gangLogoIndex,gangName);
        }
        
        public void RefreshDetectorImmediately(SceneDetectorBase sceneDetector)
        {
            SceneDetector = sceneDetector;
            if (SceneObjectLoadController != null && SceneDetector != null)
            {
                SceneObjectLoadController.RefreshDetectorImmediately(SceneDetector);
            }
        }
        
        public void ApplyRenderSetting(GgameSceneConfig mapData, Action action)
        {
            CityRenderSettingMgr.ApplyRenderSetting(mapData,action);
        }

        public void OnLodChange(int lodLevel)
        {
            ResidentHandler.Inst.GetFunction("CityLuaFunction.onLodChanged").Action(lodLevel);
        }

        public void OnZoomChange(float zoom)
        {
            bool limit = zoom >= limitZoom;
            if (limit != isLimit)
            {
                CityRealAiManager.GetInstance().ZoomChangeShowOrHideAllNpc(!limit);
                ResidentHandler.Inst.GetFunction("CityLuaFunction.OnZoomChange").Action(!limit);
                isLimit = limit;
            }

            if (innerCityCameraComponent.CurrentLod.needHideShadow)
            {
                bool shadowLimit = zoom >= limitShadowZoom;
                if (shadowLimit != isShadowLimit)
                {
                    ResidentHandler.Inst.GetFunction("CityLuaFunction.OnShadowZoomChange").Action(!shadowLimit);
                    isShadowLimit = shadowLimit;
                }
            }
            else
            {
                ResidentHandler.Inst.GetFunction("CityLuaFunction.OnShadowZoomChange").Action(true);
            }
            
        }

        public void Dispose()
        {
            //RoomDataManager.OnDispose();
            //CityTouchManager.OnDispose();
            //CityEventManager.OnDispose();
            //CityLODManager.OnDispose();
            mobileTouchCamera.OnLodChange -= OnLodChange;
            mobileTouchCamera.OnZoomChange -= OnZoomChange;
            CityRenderSettingMgr.OnDispose();
            //CityAIManager.OnDispose();
            TimelineManager.OnDispose();
        }
        
    }
}
